import React from 'react';
import FormEnhance, {InputDate, InputNumber, InputSwitch, InputText} from "common/form";
import {Report} from "type/entity/Report";
import {fetchUpdateReport} from "storage/redux/reducer/report";
import dayjs from "dayjs";
import LogFactory from "log/LogFactory";
import {Form} from "antd";
import {FormInstance} from "antd/lib/form/Form";

const log = LogFactory.getLogger("ReportUpdate");

const ReportUpdate = () => {
    const onChange = (checked: boolean, form: FormInstance<Report>) => {
        log.debug("结算状态改变:", form);
        if (checked){
            form.setFieldValue("settleTime", dayjs());
        }else {
            form.setFieldValue("settleTime", null);
        }
    };
    return (
        <FormEnhance<Report>
            title={"添加报销单"}
            backupText={"报销单列表"}
            requestAction={fetchUpdateReport}
            formDataConverter={e => {
                if (dayjs.isDayjs(e.createDate)){
                    e.createDate = e.createDate.format("YYYY-MM-DD");
                }
                if (dayjs.isDayjs(e.settleTime)){
                    e.settleTime = e.settleTime.format("YYYY-MM-DD HH:mm:ss");
                }
                return e;
            }}
            initDataConverter={initData => {
                initData.createDate = dayjs(initData.createDate, "YYYY-MM-DD");
                if (initData.settleTime){
                    initData.settleTime = dayjs(initData.settleTime, "YYYY-MM-DD HH:mm:ss");
                }
                log.debug("处理初始化数据完成", initData);
                return initData;
            }}
        >
            <InputDate<Report> name={"createDate"} label={"创建日期"} required/>
            <InputText<Report> name={"explain"} label={"报销单说明"}/>
            <InputNumber<Report> name={"totalCount"} label={"总数量"} after={"条"}/>
            <InputNumber<Report> name={"totalAmount"} label={"总金额"} after={"¥"} precision={2}/>
            <InputSwitch<Report> name={"status"} label={"结算状态"} onChange={onChange}/>
            <InputDate<Report> name={"settleTime"} label={"结算时间"} showTime disabled/>
        </FormEnhance>
    );
};

export default ReportUpdate;